<template>
  <div class="historical-data">
    <el-card :body-style="{ padding: '0px' }">
      <div slot="header">
        <tools-btn
          :toolsData="toolsData"
          @addData="addData"
          @refresh="refresh"
          @search="search"
          @fieldSet="fieldSet"
          @hasOperation="hasOperation"
        />
      </div>
      <!-- card body -->
      <table-list
        id="tableExcel"
        :table="table"
        ref="tableList"
        @handleEdit="handleEdit"
        @handleDel="handleDel"
      >
        <template
          slot-scope="props"
          slot="fan1"
        >
          <span
            :class="props.obj.row.fan1==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.fan1==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="fan2"
        >
          <span
            :class="props.obj.row.fan2==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.fan2==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="cleaner1"
        >
          <span
            :class="props.obj.row.cleaner1==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.cleaner1==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="cleaner2"
        >
          <span
            :class="props.obj.row.cleaner2==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.cleaner2==='0'?'通电':'断电'"
          />
        </template>
        <template
          slot-scope="props"
          slot="deviceStatus"
        >
          <span
            :class="props.obj.row.deviceStatus==='0'?'typeSuccsee':'typeError'"
            v-text="props.obj.row.deviceStatus==='0'?'正常':'异常'"
          />
        </template>
      </table-list>

      <pagination
        :page="page"
        @pageSizeChange="pageSizeChange"
        @pageCurrentChange="pageCurrentChange"
      />
    </el-card>

    <el-dialog
      :title="editDialog.title"
      v-if="editDialog.visible"
      :visible.sync="editDialog.visible"
      @close="closeDialog('editDialog')"
      width="30%"
    >
      <el-form
        :model="editDialog.data"
        ref="editDialog.data"
        label-width="80px"
      >
        <el-form-item label="名 称">
          <el-input
            v-model="editDialog.data.name"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="代 码">
          <el-input
            v-model="editDialog.data.num"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="图 片">
          <el-input
            v-model="editDialog.data.urlImg"
            size="mini"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button
          size="small"
          plain
          @click="closeDialog('editDialog')"
        >取 消</el-button>
        <el-popover
          class="edit-dialog-popover"
          placement="top"
          v-model="editDialog.submitVisible"
        >
          <p style="text-align: center; margin-top: 0; color:#409eff">
            确定提交吗？
          </p>
          <div style="text-align: center">
            <el-button
              size="mini"
              @click="editDialog.submitVisible = false"
              v-text="'取 消'"
              plain
            />
            <el-button
              type="primary"
              size="mini"
              v-text="'确 定'"
              @click="handleSubmit"
              plain
            />
          </div>
          <el-button
            slot="reference"
            type="primary"
            size="small"
            v-text="'提 交'"
            plain
          />
        </el-popover>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableList from "../../components/Table/TableList";
import Pagination from "../../components/Pagination/pagination";
import ToolsBtn from "../../components/Tools/ToolsBtn";
import TableProp from "../../utils/tableProps.js";
import PageProp from "../../utils/pageProps.js";
import HHead from "./HHead.js";
export default {
  name: "HistoricalData",
  data() {
    return {
      table: TableProp,
      page: PageProp,
      toolsData: {
        title: "历史数据",
        oBtnShow: [
          { btn0: false }, // 新增按钮
          { btn1: true }, // 刷新按钮
          { btn2: true }, // 搜索按钮
          { btn3: false }, // 设置按钮
          { btn4: false } // 导出Excel按钮
        ]
      },
      editDialog: {
        title: "编 辑",
        visible: false,
        submitVisible: false,
        data: null
      }
    };
  },
  components: {
    TableList,
    Pagination,
    ToolsBtn
  },
  created() {
    this.table.tHead = HHead;
    this.table.identifier = false;
    this.table.hasOperation = false;
    this.getList(1, 10);
    // this.table.data = TableDemoData;
    // this.page.total = 10;
  },
  methods: {
    // 新增数据
    addData() {
      console.log("新 增");
      this.handleEdit();
    },
    // 列表数据刷新
    refresh() {
      // console.log("刷 新");
      this.getList(1, 10);
    },
    // 搜索
    search() {
      console.log("搜 索");
    },
    // 字段设置
    fieldSet() {
      console.log("字段设置");
    },
    // 隐藏表格操作列
    hasOperation() {
      console.log("隐藏表格操作列");
      this.table.hasOperation = !this.table.hasOperation;
    },
    // 获取数据列表
    getList(pageNo, pageSize) {
      this.table.data = [];
      this.table.loading = true;
      const params = {
        pageNo,
        pageSize
      };
      this.$api.DATA.historyData(params)
        .then(res => {
          console.log(res);
          const data = res.data;
          const total = res.count;
          this.table.data = data;
          this.page.total = total;
          this.table.loading = false;
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 分页大小
    pageSizeChange(size) {
      // console.log(`每页 ${size} 条`);
      const tableDataLength = this.table.data.length;
      this.page.pageSize = size;
      if (tableDataLength < this.page.pageSize) {
        this.getList(1, size);
      } else {
        const pageNo = this.page.currentPage;
        this.getList(pageNo, size);
      }
    },
    // 翻页
    pageCurrentChange(pageNo) {
      // console.log(`当前页: ${pageNo}`);
      this.page.currentPage = pageNo;
      const size = this.page.pageSize;
      this.getList(pageNo, size);
    },
    // 编辑数据模态
    handleEdit(index, row) {
      this.editDialog.visible = true;
      if (index !== undefined && row !== undefined) {
        const title = `${row.name}`;
        const data = row;
        this.editDialog.title = title;
        this.editDialog.data = data;
      } else {
        this.editDialog.title = "添加";
        this.editDialog.data = {};
      }
    },
    // 提交数据
    handleSubmit() {
      this.editDialog.visible = false;
      this.editDialog.submitVisible = false;
      const params = this.editDialog.data;
      console.log("提交数据: ", params);
    },
    // 删除
    handleDel(index, row) {
      console.log("删除", " index", index, " row", row);
    },
    // 关闭模态
    closeDialog(name) {
      if (name === "editDialog") {
        this[name].submitVisible = false;
        this[name].data = null;
      }
      this[name].visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.edit-dialog-popover {
  margin-left: 10px;
}
.typeSuccsee {
  color: #67c23a;
}
.typeError {
  color: #f56c6c;
}
</style>
